package org.foreningsgatan.mercenaries.helpers.terrain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.foreningsgatan.mercenaries.model.terrain.TerrainMap;
import org.foreningsgatan.mercenaries.model.terrain.TerrainTile;
import org.foreningsgatan.modular.terrain.generator.TileMapGenerator;
import org.foreningsgatan.modular.terrain.model.SimpleMap;
import org.foreningsgatan.modular.terrain.model.Tile;
import org.foreningsgatan.modular.terrain.model.TileMap;


/**
 * 
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class TerrainMapGenerator {
	public static boolean match(boolean[] a, boolean [] b) {
		for (int i = 0; i < a.length; i++) {
			if (a[i] != b[i])
				return false;
		}
		
		return true;
	}
	
	private static List<Object> getObjects(Tile tile) {
		Set<Object> set = new HashSet<Object>();
		
		set.add(tile.getUpperLeft());
		set.add(tile.getUpperRight());
		set.add(tile.getLowerLeft());
		set.add(tile.getLowerRight());
		
		set.remove(null);
		
		return new ArrayList<Object>(set);
	}
	
	private static boolean[] getIsPrimary(Object primary, Tile tile) {
		boolean[] result = new boolean[4];
		
		result[0] = (tile.getUpperLeft() == primary);
		result[1] = (tile.getUpperRight() == primary);
		result[2] = (tile.getLowerLeft() == primary);
		result[3] = (tile.getLowerRight() == primary);
		
		return result;
	}
	
	public static TerrainMap generate(SimpleMap simpleMap, Map<Object, Long> textures) throws Exception {	
		TileMap tileMap = TileMapGenerator.generate(simpleMap);
		
		int size = tileMap.getSize();
		
		TerrainMap terrainMap = new TerrainMap(size);		
		
		for (int y = 0; y < size; y++) {
			for (int x = 0; x < size; x++) {
				Tile tile = tileMap.get(x, y);
				
				List<Object> objects = getObjects(tile);
				
				Object primary = objects.get(0);
				Object secondary = (objects.size() > 1) ? 
						objects.get(1) : primary;
				
				Long textureId1 = textures.get(primary);
				Long textureId2 = textures.get(secondary);
				
				if ((textureId1 == null) && (textureId2 == null)) {
					throw new Exception("Texture id not found");
				}
				
				terrainMap.set(x, y, 
						new TerrainTile(textureId1, textureId2, getIsPrimary(primary, tile)));
			}
		}
		
		return terrainMap;
	}
}
